﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using System.Web.Security;

namespace syh876.com.ajax.Account
{
    /// <summary>
    /// CheckIn 的摘要说明
    /// </summary>
    public class CheckIn : IHttpHandler, IRequiresSessionState
    {
        protected bool isRemember = false;

        public void ProcessRequest(HttpContext context)
        {
            string strCode = Common.RequestHelper.GetString("txtCode");
            string strUserName = Common.RequestHelper.GetString("txtUserName");
            string strPwd = Common.RequestHelper.GetString("txtPwd");
            isRemember = Common.RequestHelper.GetString("check") == "1" ? true : false;  //记住密码

            if (!string.IsNullOrEmpty(strCode) && !string.IsNullOrEmpty(strUserName) && !string.IsNullOrEmpty(strPwd))
            {
                if (HttpContext.Current.Session["CheckCode"] == null || String.Compare(HttpContext.Current.Session["CheckCode"].ToString(), strCode, true) != 0)
                {
                    context.Response.Write("0|验证码错误。");
                }
                else
                {
                    CheckUserInfo(strUserName, strPwd);
                }
            }
            else
            {
                context.Response.Write("0|请填写完信息,再登陆。");
            }
        }
        private void CheckUserInfo(string strUserName, string strPwd)
        {
            SYH.User.Model.UserSimplyInfo info = SYH.User.BLL.UserSimplyInfo.CheckLogin(strUserName, strPwd);

            if (info == null)
            {
                Echo("0|用户名或密码错误!");
            }
            else
            {
                //int isActive=QHW.Shop.User.BLL.User_SimplyInfo.GetIsActive(info.Id);
                //if (isActive == 1)  //已激活
                //{
                if (info.Status == 0 || info.Status == 1 || info.Status == 3)
                {
                    System.Web.Security.FormsAuthentication.SetAuthCookie(info.Id + "|" + strUserName, isRemember);

                    if (isRemember)
                    {
                        HttpContext.Current.Request.Cookies[".ASPXAUTH"].Expires = DateTime.Now.AddDays(7);
                    }

                    //FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, info.Id + "|" + strUserName, DateTime.Now, DateTime.Now.AddHours(12), true, strUserName);
                    ////加密票据
                    //string authTicket = FormsAuthentication.Encrypt(ticket);
                    ////存储为cookie
                    //HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, authTicket);
                    //cookie.Domain = "qihuiwang.cn";
                    //cookie.Expires = ticket.Expiration;
                    //HttpContext.Current.Response.Cookies.Add(cookie);
                    //判断用户是否已生成旺铺首页，否则执行生成操作
                    //var IsOpen = QHW.Shop.ShopSet.BLL.ShopUserPage.GetIsOpen(info.Id);//是否开通金铺，大于0是开通，否则没开通
                    //if (IsOpen == 0 && info.IsOpen != 0)
                    //{
                    //    var pwd = QHW.Shop.User.BLL.User_SimplyInfo.GetModel(info.Id).Password;
                    //    com.qihuiwang.zx.CreateShop manager = new com.qihuiwang.zx.CreateShop();
                    //    manager.CreateShopHtml(strUserName, pwd);
                    //}

                    Common.CookieHelper.WriteCookie("UserId", info.Id.ToString(), FormsAuthentication.CookieDomain, DateTime.Now.AddMinutes(720));

                    //记录用户登录日志
                    //try
                    //{
                    //    com.qihuiwang.monitor.MonitorService service = new com.qihuiwang.monitor.MonitorService();
                    //    service.WriteUserLoginLog(info.Id, info.Name, HttpContext.Current.Request.UserHostAddress, HttpContext.Current.Request.Browser.Browser, HttpContext.Current.Request.Browser.MajorVersion.ToString());
                    //}
                    //catch { }
                    Echo("1|登陆成功!");

                }
                else if (info.Status == 2)
                {
                    Echo("0|您的生意汇账户已被关闭!");
                }
                //}
                //else if(isActive==0)
                //{
                //    Echo("2|账户未激活，请登陆您注册时的邮箱进行激活，"+info.Email.ToString()); 
                //}
            }



        }
        //输出开始
        private void Echo(string s)
        {
            HttpContext.Current.Response.Write(s);
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}